﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlackBox.Models
{
    public class KeyboardWarriorsRepository
    {
        DataClassesDataContext db = new DataClassesDataContext();

        //SingleTon patern
        public static KeyboardWarriorsRepository instance;


        public static KeyboardWarriorsRepository Instance
        {
            get
            {
                if (instance == null)
                    instance = new KeyboardWarriorsRepository();
                return instance;
            }
        }

        public CharacterSheet getCharacterSheetByPlayer(String player)
        {
            CharacterSheet returnSheet = (from x in db.CharacterSheets
                                          where x.player == player
                                          select x).Single();
            return returnSheet;
        }
        public CharacterSheet getCharacterSheetById(int id)
        {
            CharacterSheet returnSheet = (from x in db.CharacterSheets
                                          where x.id == id
                                          select x).Single();
            return returnSheet;
        }
        public void addCharacterSheet(CharacterSheet characterSheetToBeAdded)
        {
            db.CharacterSheets.InsertOnSubmit(characterSheetToBeAdded);
            db.SubmitChanges();
        }
        public void deleteCharacterSheet(CharacterSheet characterSheetToBeDeleted)
        {
            db.CharacterSheets.DeleteOnSubmit(characterSheetToBeDeleted);
            db.SubmitChanges();
        }
        public void updateCharacterSheet(CharacterSheet updateForCharacterSheet)
        {
            CharacterSheet characterSheetToBeUpdated = (from x in db.CharacterSheets
                                                        where x.player == updateForCharacterSheet.player
                                                        select x).Single();
            characterSheetToBeUpdated = new CharacterSheet
            {
                name = updateForCharacterSheet.name,
                namePostFix = updateForCharacterSheet.namePostFix,
                strength = updateForCharacterSheet.strength,
                dexterity = updateForCharacterSheet.dexterity,
                intelligence = updateForCharacterSheet.intelligence,
                hitPoints = updateForCharacterSheet.hitPoints,
            };
            db.SubmitChanges();
        }

    }
}